function structure = calling_tree(filename, restriction, cell)
%This function shows the tree of
%functions that is called by filename.
%Input:     -String
%           -String, search directory, files outside directory will not be checked
%           '' = no restrictions.
%           Restriction can contain a part of the path: so only work will suffice if
%           you want to search only in the work directory.
%           -Cell, should just be left out, filled in by program
%Output:    -Structure, containing complete data structure
%Mark Jobse 08-01-2003

if nargin < 3
    [path, FileName, ext] = fileparts(filename);
    cell{1} = FileName;
end
if isempty(cell{1})
    [path, FileName, ext] = fileparts(filename);
    cell{1} = FileName;
end

if nargin < 2
    restriction = '';
end

filenames = calling(filename);
structure = [];
%Check which functions are built in and expand the rest
array = [];
array2 = [];
if ~isempty(filenames(1).Name)
    for i = 1:length(filenames)
        if isempty(findstr(filenames(i).Location, 'built-in'))
            array = append(array, i);
        else
            array2 = append(array2, i);
        end
	end
	
	for i = 1:length(array)
        %Remove extension
        Name = filenames(array(i)).Name
        Location = filenames(array(i)).Location;
        Location = append(Location, '\');
%         pos = findstr('.', Name);
        fieldname = Name;
        already_in_cell = 0;
        for i = 1:length(cell)
            if isequal(cell{i}, Name)
                already_in_cell = 1;
            end
        end
        if already_in_cell
            structure = setfield(structure, fieldname, 'Self-call');    
        else
            cell2 = cell;
            cell2{end+1} = Name;
            [FullPath, FullName, Ext] = fileparts(which(Name));
            if isempty(findstr(FullPath, restriction)) & ~isempty(FullPath) & ~isempty(restriction);
                structure = setfield(structure, fieldname, 'Outside');
            else
                structure = setfield(structure, fieldname, calling_tree(which(Name), restriction, cell2));
            end
        end
	end
	
	for i = 1:length(array2)
        %Remove extension
        Name = filenames(array2(i)).Name;
        Location = filenames(array2(i)).Location;
	%    pos = findstr('.', Name);
        fieldname = Name;
        structure = setfield(structure, fieldname, 'Built-in');
	end
else
    structure = char(32);
end